package com.qubuyer.customview

import android.app.Activity
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.qubuyer.R
import com.qubuyer.core.utils.StatusBarUtil

class TitleBar @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private var statusBarHeight = 0
    private var actionBarSize = 0
    private var naviIcon = 0
    private var rightIcon = -1
    private var title: String? = null
    private var naviImageView: ImageView? = null
    private var rightImageView: ImageView? = null
    private var titleView: TextView? = null

    init {
        val a0 = context.obtainStyledAttributes(attrs, R.styleable.TitleBar, defStyleAttr, 0)
        val includeStatusBar = a0.getBoolean(R.styleable.TitleBar_includeStatusBar, false)
        naviIcon = a0.getResourceId(R.styleable.TitleBar_naviIcon, R.drawable.icon_back_white)
        rightIcon = a0.getResourceId(R.styleable.TitleBar_rightIcon, -1)
        title = a0.getString(R.styleable.TitleBar_title)
        a0.recycle()
        if (includeStatusBar) {
            statusBarHeight = StatusBarUtil.getStatusBarHeight()
            if (context is Activity) {
                StatusBarUtil.transparentStatusBar(context)
            }
            setPadding(0,statusBarHeight,0,0)
        }

        val typedValue = TypedValue()
        context.theme.resolveAttribute(android.R.attr.actionBarSize, typedValue, true)
        val a: TypedArray = context.obtainStyledAttributes(typedValue.data, intArrayOf(R.attr.actionBarSize))
        actionBarSize = a.getDimensionPixelSize(0, -1)
        a.recycle()
        layoutParams = LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, actionBarSize)

        if (background == null) {
            setBackgroundResource(R.drawable.shape_title_bar_bg)
        }
        addChildren()
    }

    private fun addChildren() {
        addView(ImageView(context).apply {
            layoutParams = LayoutParams(actionBarSize, actionBarSize, Gravity.BOTTOM)
            setImageResource(naviIcon)
            scaleType = ImageView.ScaleType.CENTER_INSIDE
            naviImageView = this
        })
        addView(TextView(context).apply {
            layoutParams = LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, actionBarSize, Gravity.BOTTOM).apply {
                leftMargin = actionBarSize
                rightMargin = actionBarSize
            }
            gravity = Gravity.CENTER
            setSingleLine()
            setTextColor(Color.WHITE)
            text = title
            textSize = 16f
            titleView = this
        })
        if(rightIcon != -1){
            addView(ImageView(context).apply {
                layoutParams = LayoutParams(actionBarSize, actionBarSize, Gravity.BOTTOM or Gravity.END)
                setImageResource(rightIcon)
                scaleType = ImageView.ScaleType.CENTER_INSIDE
                rightImageView = this
            })
        }
    }

    fun setOnNaviClick(block: (() -> Unit)?) {
        naviImageView?.setOnClickListener { block?.invoke() }
    }

    fun setOnRightClick(block: (() -> Unit)?) {
        rightImageView?.setOnClickListener { block?.invoke() }
    }

    fun setTitle(text : String?){
        this.title = text
        this.titleView?.text = text
    }
}